///////////////////////////////////////////////////////////////////////////////////////
//
//	DoD Anti Score Reset
//		- Version 1.0
//		- 11.30.2007
//		- diamond-optic
//
//////////////////////////////////////////////////////////////////////////////////
//
// Information:
//
//	- Saves clients scores so if they reconnect before
//	  the map has changed, they will continue with the
//	  score they had when they left.
//
//	- Prevents clients from reconnecting to reset their awful scores
//	- Saved information is removed on map change
//
//	- By default, 256 steam ids are stored before it starts
//	  overwriting. This should be more then plenty to stop
//	  players from reseting their scores.
//
//	- Increasing the max stored names (MAX_USERS define) could 
//	  allow you to save the score for almost anyone that played
//	  as long as they rejoin before the map has changed
//
//////////////////////////////////////////////////////////////////////////////////
//
// CVARs:
//
//	dod_antireset "1"	// Enable/Disable (1/0)
//
//////////////////////////////////////////////////////////////////////////////////
//
// Changelog:
//
// 	- 11.30.2007 Version 1.0
//		Initial Release
//
//////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////
// Includes
//
#include <amxmodx>
#include <dodfun>
#include <dodx>

/////////////////////////////////////////////////
// Version
//
#define VERSION "1.0"

/////////////////////////////////////////////////
// Max steamids/scores to save
//
#define MAX_USERS 256

new dataSaved[MAX_USERS][3]
new dataAuth[MAX_USERS][32]
new dataPlace[33]
new dataLast

new p_plugin

public plugin_init()
{
	register_plugin("DoD Anti Score Reset",VERSION,"AMXX DoD Team")
	register_cvar("dod_antireset_stats",VERSION,FCVAR_SERVER|FCVAR_SPONLY)
		
	p_plugin = register_cvar("dod_antireset","1")
	
	register_event("ResetHUD", "player_spawn", "be")
}

public client_putinserver(id)
{
	if(get_pcvar_num(p_plugin) && is_user_connected(id) && !is_user_bot(id))
		set_task(5.0,"check_data",id)
}

public check_data(id)
{	
	if(is_user_connected(id))
		{
		new authid[32]
		get_user_authid(id,authid,31)
		
		for(new i = 0; i < MAX_USERS; i++)
			{
			if(equali(dataAuth[i],authid))
				{
				dod_set_user_score(id,dataSaved[i][0])
				dod_set_user_kills(id,dataSaved[i][1])
				dod_set_pl_deaths(id,dataSaved[i][2])
				
				dataPlace[id] = i
				
				return PLUGIN_CONTINUE
				}
			}
			
		dataPlace[id] = 0
		
		}
		
	return PLUGIN_CONTINUE
}

public player_spawn(id)
{
	if(get_pcvar_num(p_plugin) && is_user_connected(id) && !is_user_bot(id))
		{
		new score = dod_get_user_score(id)
		new kills = dod_get_user_kills(id)
		new deaths = dod_get_pl_deaths(id)
			
		new authid[32]
		get_user_authid(id,authid,31)
		
		if(dataPlace[id])
			{
			if(equali(dataAuth[dataPlace[id]],authid))
				{
				dataSaved[dataPlace[id]][0] = score
				dataSaved[dataPlace[id]][1] = kills
				dataSaved[dataPlace[id]][2] = deaths
				
				return PLUGIN_CONTINUE
				}
			}
		else
			{			
			for(new i = 0; i < MAX_USERS; i++)
				{
				if(equali(dataAuth[i],authid))
					{
					dataSaved[i][0] = score
					dataSaved[i][1] = kills
					dataSaved[i][2] = deaths
					
					dataPlace[id] = i
					
					return PLUGIN_CONTINUE
					}
				}
			
			dataAuth[dataLast] = authid
			dataSaved[dataLast][0] = score
			dataSaved[dataLast][1] = kills
			dataSaved[dataLast][2] = deaths
			
			if(dataLast >= MAX_USERS-1)
				dataLast = 0
			else
				dataLast++
			}
		}
		
	return PLUGIN_CONTINUE
}
